package com.hy.controller;

import com.hy.Result.ResponseResult;
import com.hy.common.Result;
import com.hy.pojeVo.QueryParam;
import com.hy.pojo.vaccine.Vaccine;
import com.hy.service.VaccineService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/app/vaccine")
public class AppVaccinationController {

    @Autowired
    private VaccineService vaccineService;
    @PostMapping("/page")
    public ResponseResult searchPage(@RequestBody QueryParam queryParam) {
        return vaccineService.searchWord(queryParam);
    }

    /**
     * 根据id查询疫苗
     */
    @GetMapping("/getById")
    public Result getById(@RequestParam("id") Long id) {
        log.info("根据id查询疫苗，id：{}", id);
        Vaccine vaccine = vaccineService.getById(id);
        return Result.success(vaccine);
    }


    /**
     * 新增疫苗
     */
    @PostMapping("/save")
    public Result save(@RequestBody Vaccine vaccine) {
        log.info("新增疫苗：{}", vaccine);
        vaccineService.save(vaccine);
        return Result.success(vaccine);
    }

    /**
     * 根据id删除疫苗
     */
    @DeleteMapping("/removeById")
    public Result<String> delete(@RequestParam("id") Long id) {
        log.info("删除疫苗，id：{}", id);
        vaccineService.removeById(id);
        return Result.success("删除成功");
    }

    /**
     * 修改疫苗信息
     */
    @PutMapping("/updateById")
    public Result update(@RequestBody Vaccine vaccine) {
        try {
            System.out.println(vaccine);
            log.info("修改疫苗：{}", vaccine);
            vaccineService.updateById(vaccine);
            return Result.success("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("修改失败!"+e.getMessage());
        }
    }



}
